<?php
/**
 * @user:andy
 * @date:2021/6/23
 * @company:http://www.sjdphp.com
 * @desc: 树形结构
 */

namespace app\common\server;


class Tree
{
    /** 处理菜单数据
     * @param $arr
     * @param int $pid
     * @return array
     */
    public static function getMenuTree($arr,$pid = 0){
        $res = [];
        foreach($arr as $v){
            if($v["pid"] == $pid){
                $v["child"] = self::getMenuTree($arr,$v["menu_id"]);
                if(empty($v["child"])){
                    unset($v["child"]);
                }
                $res[] = $v;
            }
        }
        return $res;
    }

    /** 获取layui树形数据
     * @param $arr
     * @param int $pid
     * @return array
     */
    public static function getLayerTree($arr,$pid = 0){
        $res = [];
        foreach($arr as $v){
            if($v["pid"] == $pid){
                $str['title'] = $v['title'];
                $str['id']    = $v['id'];
                $str['children'] = self::getLayerTree($arr,$v['id']);
                if(empty($str['children'])){
                    unset($str['children']);
                }
                $res[] = $str;
            }
        }
        return $res;
    }


    public static function getTree($arr,$pid = 0,$leftHtml = '--',$level = 0){
        $res = [];
        foreach($arr as $v){
            if($v["pid"] == $pid){
                $v["level"] = $level + 1;
                $v["menu_name"] = str_repeat($leftHtml,$level) . $v["menu_name"];
                $res [] = $v;
                $res = array_merge($res,self::getTree($arr,$v["menu_id"],$leftHtml = '--',$level + 1));
            }
        }
        return $res;
    }


    public static function getPlateTree($arr,$pid = 0,$leftHtml = '--',$level = 0){
        $res = [];
        foreach($arr as $v){
            if($v["pid"] == $pid){
                $v["level"] = $level + 1;
                $v["plate_name"] = str_repeat($leftHtml,$level) .' '. $v["plate_name"];
                $res[] = $v;
                $res = array_merge($res,self::getPlateTree($arr,$v["plate_id"],$leftHtml = '--',$level + 1));
            }
        }
        return $res;
    }

    public static function getNavTree($arr,$pid = 0,$leftHtml = '--',$level = 0){
        $res = [];
        foreach($arr as $v){
            if($v["pid"] == $pid){
                $v["level"] = $level + 1;
                $v["nav_name"] = str_repeat($leftHtml,$level) .' '. $v["nav_name"];
                $res[] = $v;
                $res = array_merge($res,self::getNavTree($arr,$v["nav_id"],$leftHtml = '--',$level + 1));
            }
        }
        return $res;
    }


    public static function getBoroughsLists($arr,$pid = 0,$leftHtml = '--',$level = 0){
        $res = [];
        foreach($arr as $v){
            if($v["pid"] == $pid){
                $v["level"] = $level + 1;
                $v["boroughs_name"] = str_repeat($leftHtml,$level) .' '. $v["boroughs_name"];
                $res[] = $v;
                $res = array_merge($res,self::getBoroughsLists($arr,$v["boroughs_id"],$leftHtml = '--',$level + 1));
            }
        }
        return $res;
    }

//    public static function getTreeLists($arr,$pid = 0,$leftHtml = '--',$level = 0,$model_id = '',$model_name = ''){
//        $res = [];
//        foreach($arr as $v){
//            if($v["pid"] == $pid){
//                $v["level"] = $level + 1;
//                $v[$model_name] = str_repeat($leftHtml,$level) .' '. $v[$model_name];
//                $res[] = $v;
//                $res = array_merge($res,self::getTreeLists($arr,$v[$model_id],$leftHtml = '--',$level + 1,$v[$model_id],$v[$model_name]));
//            }
//        }
//        return $res;
//    }
}